﻿
namespace QdArch
{
    public  class WindowAction3D :Element3DAction
    {
        public static WindowAction3D Instance = new WindowAction3D();
        public const double frameWidth = 50;
        public override BufferGeometry[] CreateGeometry3D(LcLevel level, LcElement element, Matrix3 matrix)
        {
            var window = element as QdWindowRef;
            var thickness=window.Thickness;
            var glassThickness = 10;
            var bottom = window.Bottom;
            var height = window.Height;
            var width = window.Width;
            var start2 = window.StartPoint;
            var start = new Vector3(start2, bottom);
            var end2 = window.EndPoint;
            var end = new Vector3(end2, bottom);
            var lineVec = new Vector3().SubVectors(end, start);
            var len = lineVec.Length();
            var xAxis = lineVec.Clone().Normalize();
            var yAxis = VectorUtil.ZAxis.Clone();
            var zAxis = new Vector3().CrossVectors(xAxis, yAxis).Normalize();
            var coodMat = new Matrix4();
            coodMat.MakeBasis(xAxis, yAxis, zAxis);
            coodMat.SetPosition(start);
            var glassShape = ShapeUtil.Rect(frameWidth, frameWidth, len- frameWidth, height- frameWidth);
            var frameShape = ShapeUtil.Rect(0, 0, len, height);
            frameShape.holes.Add(glassShape);
            var glassGeo= GeoUtil.GetStretchGeometryData(glassShape, coodMat, glassThickness / 2, -glassThickness / 2).GetBufferGeometry();
            glassGeo.computeVertexNormals();
            var frameGeo = GeoUtil.GetStretchGeometryData(frameShape, coodMat, thickness / 2, -thickness / 2).GetBufferGeometry();
            frameGeo.computeVertexNormals();
            return new BufferGeometry[] { frameGeo,glassGeo };
        }
    }
}
